// 引入react
import React, {PureComponent} from 'react';
// 引入核心grid组件
import ScGridTable from '../../../components/display/table/gridTableCore';
// 定义业务的接口对象
import {getRoleList, getPermissionList, getUserMenuList} from '../../../fetch/fetchApi';
import PropTypes from 'prop-types';
import {initModals} from "../../../state/action/comAction";
import fetchHandler from '../../../fetch/fetchHandler';
import RefreshGrid from '../../../utils/refreshGrid';

class TransferTable extends PureComponent{
  constructor (props) {
    super(props);
    // 获取父组件传递的参数
    this.props = props;
  }

  static contextTypes = {
    store: PropTypes.object.isRequired
  }

  componentWillMount () {
    // 初始定义表格数据
    this.generateSchema();

    // 初始化相关参数
    this.initParams();
  }

  // 表格列的定义
  generateSchema () {
    this.setState({
      tableConfig : {
        tableColumns : [
          {
            name: '角色名称',
            key: 'title',
            fix: 'left'
          },
          {
            name: '操作',
            key: 'action',
            colType: 'action', // 操作列，可定义多种操作。
            actionMap: [
              {
                type: 'href',
                actionName: '编辑',
                handler: (res) => {
                  this.openTransfer(res);
                }
              },
              {
                type: 'confirm',
                actionName: '删除',
                handler: (res) => {
                  this.dispathRole(res);
                }
              }
            ]
          }
        ],
        tableParam: {
          pageCount: 5
        },
        tableInfo: {
          hasRow: !1 // 是否显示checkbox
        },
        tableChange: () => {
          // grid翻页页，排序， 筛选过滤时触发
          console.log('change');
        },
        dataApi: getRoleList
      }
    });
  }

  initParams () {
    fetchHandler(getPermissionList, {}).then((data) => {
      this.dataSource = JSON.parse(JSON.stringify(data.data).replace(/id/g,'key').replace(/childList/g, 'children'));
    });
    fetchHandler(getUserMenuList, {}).then((data) => {
      this.treeSource = JSON.parse(JSON.stringify(data.data).replace(/id/g,'key').replace(/childList/g, 'children'));
    });
  }

  // 自定义事件， 用户自行定义对应业务逻辑

  openTransfer (res) {
    // 封装参数 (根据数据类型自行format)
    const targetArr = res.paramStr ? res.paramStr.split(',') : [];
    // 树状参数封装
    const targetTree = res.treeStr ? res.treeStr.split(',') : [];
    const param = {
      defaultSource: this.dataSource,
      defaultTarget: targetArr,
      treeSource: this.treeSource,
      treeTarget: targetTree
    };
    // 打开transferModal
    this.context.store.dispatch(initModals('transferModal', !0, param));
  }

  dispathRole (res) {
    fetchHandler(getUserMenuList, {id: res.key}).then((res) => {
      if (res && res.code === 'success') {
        // 调用当前grid刷新
        RefreshGrid(this.context.store);
      }
    });
  }

  render () {
// 用户只需要传入接口对象和配置的列信息即可
    return <ScGridTable config={this.state.tableConfig} />;
  }

}

// 最后我们得到自定义的gridTable,放置到业务page中。
export default TransferTable;